using Godot;
using System;
using System.Threading.Tasks;
public partial class Manger : Node2D
{
	[Export]
	private Player _p;
	[Export]
	private Emeny _e;
	[Export]
	private AnimationPlayer _showWhiteCard;
	[Export]
	private Sprite2D bossPhoto;
	[Export]
	private Sprite2D finalPhoto;
	[Export]
	private Sprite2D whitePhoto;
	[Export]
	private Label _emenyPeopleHPText;
	[Export]
	private AnimatedSprite2D _animatedWn;
	[Export]
	private AnimatedSprite2D _boy;
	[Export]
	private AnimatedSprite2D _girl;
	[Export]
	private Sprite2D w1;
	[Export]
	private Sprite2D w2;
	[Export]
	private Sprite2D w3;
	[Export]
	private Sprite2D w4;
	[Export]
	private AnimationPlayer reBack;
	
	[Export]
	private Label _playPeopleHPText;
	[Export]
	private Sprite2D _bigTemplate;
	[Export]
	private Label[] _trapNum;
	private Tween.EaseType EaseType { get; set; } = Tween.EaseType.Out;
	private Tween.TransitionType TransitionType { get; set; } = Tween.TransitionType.Quad;
	private int count = 0;
	private bool waitTag = false;
	private Tween _moveTween;
	[Signal]
	public delegate void MySignalEventHandler(string message);
	[Signal]
	public delegate void nextEnergySignalEventHandler(int stages);
	[Signal]
	public delegate void endOfRoundSignalEventHandler(int stages);
	[Signal]
	public delegate void MyDiaSignalEventHandler(string word, int i);
	[Signal]
	public delegate void finalCardSignEventHandler(int tag);
	private int kunkun = 0;
	private int trapGrade2 = 0;
	private int trapGrade4 = 0;
	private int trapGrade6 = 0;
	private int trapGrade8 = 0;
	private int trapGrade10 = 0;
	private int kjysAtkSum = 0;
	private int kjysDefSum = 0;
	private int GnszAtkSum = 0;
	private int GnszDefSum = 0;
	private int FbszAtkSum = 0;
	private int FbszDefSum = 0;
	private int btszEnemy = 0;
	private Random random = new Random();
	private int duGuaiRate = 50;
	public override void _Ready()
	{

	}
	public void useMagicSignalHand(Card p1, int grade)
	{
		if (grade == 2)
		{
			while (_p.handCardsNum < 6)
			{
				_p.DrawCard(_p.handCardsNum);
			}
		}
		else if (grade == 4)
		{
			for (int i = 0; i < 5; i++)
			{
				if (_p.upCards[i] != null && _p.upCards[i].name == "小约翰可汗")
				{
					_p.upCards[i].extraAttackTimes++;
				}
			}
		}
		else if (grade == 6)
		{
			for (int i = 0; i < 5; i++)
			{
				if (_p.upCards[i] != null)
				{
					_p.upCards[i].changeAD(_p.upCards[i].atk + _p.upCards[i].grade * 500, _p.upCards[i].def + _p.upCards[i].grade * 500);
				}
			}
		}
		else if (grade == 8)
		{
			for (int i = 0; i < 5; i++)
			{
				if (_p.upCards[i] != null)
				{
					var j = random.Next(1, 101);
					if (j <= duGuaiRate)
					{
						_p.upCards[i].changeAD(_p.upCards[i].atk * 2, _p.upCards[i].def * 2);
					}
				}
			}
		}
		else if (grade == 10)
		{
			for (int i = 0; i < 5; i++)
			{
				if (_p.upCards[i] != null)
				{
					_p.upCards[i].extraAttackTimes++;
				}
			}
		}
		changeCardStatus(0);
	}
	public void useTrapSignalHand(Card p1, int grade)
	{
		if (grade == 2)
		{
			trapGrade2++;
			_trapNum[0].Text = "X" + trapGrade2;
		}
		else if (grade == 4)
		{
			trapGrade4++;
			_trapNum[1].Text = "X" + trapGrade4;
		}
		else if (grade == 6)
		{
			trapGrade6++;
			_trapNum[2].Text = "X" + trapGrade6;
		}
		else if (grade == 8)
		{
			duGuaiRate += 20;
			_trapNum[3].Text = "X" + trapGrade8;
		}
		else if (grade == 10)
		{
			trapGrade10++;
			_trapNum[4].Text = "X" + trapGrade10;
		}
		else
		{

		}
		changeCardStatus(0);
	}
	public void useMonstSignalHand(Card p1, int grade)
	{
		if (grade == 2)
		{
			p1.changeAD(p1.atk + kunkun * 300, p1.def + kunkun * 300);
			kunkun++;
		}
		changeCardStatus(0);
	}
	public void showCardSignalHandler(int grade, Card node)
	{
		changeCardStatus(1);
	}
	public async void startFight()
	{
		var ww = 0;
		var playCard = _p.upCards;
		var emenyCard = _e.emenyCards;
		changeCardStatus(1);
		if(count==8){
			for (int i = 0; i < 5; i++){
				if (playCard[i] != null)
				{
					_p.removeUpPlayerCard(playCard[i], i);
				}
			}
			
			bossPhoto.Visible=false;
			finalPhoto.Visible=true;
			await ToSignal(GetTree().CreateTimer(2.5), "timeout");
			EmitSignal(SignalName.MyDiaSignal, "", 6);
			waitTag=true;
			while(waitTag){
				await ToSignal(GetTree(), "process_frame");
			}
			finalPhoto.Visible=false;
			bossPhoto.Visible=true;
			
			
			await ToSignal(GetTree().CreateTimer(1), "timeout");
			EmitSignal(SignalName.MyDiaSignal, "", 7);
			waitTag=true;
			while(waitTag){
				await ToSignal(GetTree(), "process_frame");
			}
			
			
			bossPhoto.Visible=false;
			whitePhoto.Visible=true;
			w1.Visible=true;
			w2.Visible=true;
			w3.Visible=true;
			w4.Visible=true;
			reBack.Play("ReBack");
			await ToSignal(GetTree().CreateTimer(6), "timeout");
			EmitSignal(SignalName.MyDiaSignal, "", 8);
			waitTag=true;
			while(waitTag){
				await ToSignal(GetTree(), "process_frame");
			}
			w1.Visible=false;
			w2.Visible=false;
			w3.Visible=false;
			w4.Visible=false;
			bossPhoto.Visible=true;
			whitePhoto.Visible=false;
			
			
			await ToSignal(GetTree().CreateTimer(1), "timeout");
			EmitSignal(SignalName.MyDiaSignal, "", 9);
			waitTag=true;
			while(waitTag){
				await ToSignal(GetTree(), "process_frame");
			}
			
			bossPhoto.Visible=false;
			whitePhoto.Visible=true;
			_animatedWn.Visible=true;
			_animatedWn.Play("wn");
			await ToSignal(GetTree().CreateTimer(2.5), "timeout");
			EmitSignal(SignalName.MyDiaSignal, "", 10);
			waitTag=true;
			while(waitTag){
				await ToSignal(GetTree(), "process_frame");
			}
			_animatedWn.Visible=false;
			_girl.Visible=true;
			_girl.Play("girl");
			await ToSignal(GetTree().CreateTimer(2.5), "timeout");
			EmitSignal(SignalName.MyDiaSignal, "", 11);
			waitTag=true;
			while(waitTag){
				await ToSignal(GetTree(), "process_frame");
			}
			_girl.Visible=false;
			
			
			_boy.Visible=true;
			_boy.Play("boy");
			await ToSignal(GetTree().CreateTimer(2.5), "timeout");
			EmitSignal(SignalName.MyDiaSignal, "", 12);
			waitTag=true;
			while(waitTag){
				await ToSignal(GetTree(), "process_frame");
			}
			_boy.Visible=false;
			
			bossPhoto.Visible=true;
			whitePhoto.Visible=false;
			EmitSignal(SignalName.finalCardSign,0);
			EmitSignal(SignalName.MyDiaSignal, "", 13);
			waitTag=true;
			while(waitTag){
				await ToSignal(GetTree(), "process_frame");
			}
			
			
			Tween tween = CreateTween();
			tween.SetParallel(true);
			tween.TweenProperty(_p.finalCards[1], "position", _p.finalCards[2].Position , 1.0f).SetTrans(TransitionType).SetEase(EaseType);
			tween.TweenProperty(_p.finalCards[3], "position", _p.finalCards[2].Position , 1.0f).SetTrans(TransitionType).SetEase(EaseType);
			tween.Play();
			await ToSignal(GetTree().CreateTimer(1.5), "timeout");
			_bigTemplate.Texture = GD.Load<Texture2D>("res://photo/card/high/超高校级一家.png");
			_bigTemplate.Visible=true;
			EmitSignal(SignalName.MyDiaSignal, "", 14);
			waitTag=true;
			while(waitTag){
				await ToSignal(GetTree(), "process_frame");
			}
			
			
			bossPhoto.Visible=false;
			finalPhoto.Visible=true;
			await ToSignal(GetTree().CreateTimer(2.5), "timeout");
			EmitSignal(SignalName.MyDiaSignal, "", 15);
			waitTag=true;
			while(waitTag){
				await ToSignal(GetTree(), "process_frame");
			}
			finalPhoto.Visible=false;
			bossPhoto.Visible=true;
			
			GetTree().ChangeSceneToFile("res://scene/menu.tscn");
			return ;
			
		}
		//给玩家初始化攻击次数，将场上卡片的监听状态关闭
		for (int t = 0; t < 5; t++)
		{
			if (playCard[t] != null)
			{
				ww++;
				playCard[t].attackTimes = 1 + playCard[t].extraAttackTimes;
				playCard[t].status = 1;
				if (playCard[t].name == "诸葛琴魔")
				{
					for (int i = 0; i < 5; i++)
					{
						if (playCard[i] != null)
						{
							if (playCard[i].name == "诸葛琴魔") continue;
							playCard[i].changeAD(playCard[i].atk + 500, playCard[i].def + 500);
						}

					}
				}
			}
		}
		//给敌方初始化攻击次数
		if (trapGrade6 > 0)
		{
			trapGrade6--;
			_trapNum[2].Text = "X" + trapGrade6;
		}
		else
		{
			for (int t = 0; t < 5; t++)
			{
				if (emenyCard[t] != null)
				{
					emenyCard[t].attackTimes = 1;
				}
			}
		}

		var w = 0;
		//特殊敌人效果监测
		for (int t = 0; t < 5; t++)
		{
			if (emenyCard[t] != null)
			{
				if (emenyCard[t].kjysTag)
				{
					w++;
				}
				else if (emenyCard[t].kjysSumTag)
				{
					emenyCard[t].kjysSumTag = false;
					emenyCard[t].atk = kjysAtkSum;
					emenyCard[t].def = kjysDefSum;
				}
				else if (emenyCard[t].sexyMagic)
				{
					if (ww != 0)
					{
						int m = random.Next(0, ww);
						int n = 0;
						while (playCard[n] == null || playCard[n].capacityDef) n++;
						while (m > 0)
						{
							n++;
							while (playCard[n] == null || playCard[n].capacityDef) n++;
							m--;
						}
						emenyCard[t].changeAD(playCard[n].atk, emenyCard[t].def);
						playCard[n].changeAD(0, playCard[n].def);
					}
					emenyCard[t].sexyMagic = false;
				}
				else if (emenyCard[t].sixMagicTag)
				{
					emenyCard[t].attackTimes = 5;
				}
				else if (emenyCard[t].mixMagic)
				{
					for (int i = 0; i < 5; i++)
					{
						if (playCard[i] != null && !playCard[i].capacityDef)
						{
							_p.removeUpPlayerCard(playCard[i], i);
							playCard[i] = null;
						}
					}
					emenyCard[t].mixMagic = false;
				}
				else if (emenyCard[t].name == "光能使者")
				{
					emenyCard[t].changeAD(emenyCard[t].atk + 1000, emenyCard[t].def);
				}
				else if (emenyCard[t].name == "风暴使者")
				{
					emenyCard[t].changeAD(emenyCard[t].atk, emenyCard[t].def + 1000);
				}
				else if (emenyCard[t].name == "波涛使者")
				{
					btszEnemy = 1;
				}
				else if (emenyCard[t].name == "超级波涛使者")
				{
					btszEnemy = 3;
				}
				else if (emenyCard[t].name == "超级光能使者")
				{
					emenyCard[t].changeAD(emenyCard[t].atk + GnszAtkSum + 2000, GnszDefSum + emenyCard[t].def);
					GnszAtkSum = 0;
					GnszDefSum = 0;
				}
				else if (emenyCard[t].name == "超级风暴使者")
				{
					emenyCard[t].changeAD(emenyCard[t].atk + FbszAtkSum, emenyCard[t].def + 2000 + FbszDefSum);
					FbszAtkSum = 0;
					FbszDefSum = 0;
				}
				else
				{

				}

			}
		}
		if (w > 0)
		{
			for (int t = 0; t < 5; t++)
			{
				if (emenyCard[t] != null && emenyCard[t].kjysTag)
				{
					emenyCard[t].trueAtk = emenyCard[t].trueAtk+w * 200;
					emenyCard[t].trueDef  =emenyCard[t].trueDef+ w * 200;
					emenyCard[t].changeAD(emenyCard[t].atk + w * 200, emenyCard[t].def + w * 200);
				}
			}
		}
		//调用战斗逻辑
		bool tag = true;
		while (tag)
		{
			tag = await startFightChildren(playCard, emenyCard);
		}
		EmitSignal(SignalName.endOfRoundSignal, 0);
	}

	public void handCardCheck()
	{
		if (_p.handCardsNum > 6)
		{
			changeCardStatus(1);
			EmitSignal(SignalName.MyDiaSignal, "手牌数大于6，需要丢弃手牌", 1);
		}
		else
		{
			EmitSignal(SignalName.endOfRoundSignal, 1);
		}
	}

	public void endDigia()
	{
		changeCardStatus(2);
	}

	public void endStop()
	{
		waitTag = false;
	}

	public void nextSuceessSignal()
	{
		if (_p.handCardsNum <= 6)
		{
			EmitSignal(SignalName.endOfRoundSignal, 1);
		}
	}

	public void changeCardStatus(int statusTag)
	{
		for (int t = 0; t < _p.handCardsNum; t++)
		{
			_p.handCards[t].status = statusTag;
		}
		for (int t = 0; t < 5; t++)
		{
			if (_p.upCards[t] != null)
			{
				_p.upCards[t].status = statusTag;
			}
		}
	}

	public async Task<bool> startFightChildren(Card[] playCard, EmenyCard[] emenyCard)
	{
		bool tag = false;
		int i = 0, j = 0;
		while (i < 5 && (playCard[i] == null || playCard[i].attackTimes <= 0)) i++;
		while (j < 5 && emenyCard[j] == null) j++;
		if (i < 5 && j < 5 && !playCard[i].penetrate)
		{
			tag = true;
			BeforeFight(playCard[i], emenyCard[j]);
			if (i < j)
			{
				await ToSignal(GetTree().CreateTimer(0.5f), "timeout");
				MoveToPosition(playCard[i], emenyCard[j].GlobalPosition + new Vector2(50, 50), -20);
				await ToSignal(GetTree().CreateTimer(2.0), "timeout");
			}
			else if (i == j)
			{
				await ToSignal(GetTree().CreateTimer(0.5f), "timeout");
				MoveToPosition(playCard[i], emenyCard[j].GlobalPosition + new Vector2(0, 0), 0);
				await ToSignal(GetTree().CreateTimer(2.0), "timeout");
			}
			else
			{
				await ToSignal(GetTree().CreateTimer(0.5f), "timeout");
				MoveToPosition(playCard[i], emenyCard[j].GlobalPosition + new Vector2(50, 50), 20);
				await ToSignal(GetTree().CreateTimer(2.0), "timeout");

			}
			if (emenyCard[j].def < playCard[i].atk)
			{
				if (btszEnemy > 0)
				{
					btszEnemy--;
					AfterFight(playCard[i], emenyCard[j]);
				}
				else
				{
					AfterFight(playCard[i], emenyCard[j]);
					if (playCard[i].name == "卢本伟")
					{
						playCard[i].changeAD(playCard[i].atk + 2000, playCard[i].def);
					}
					if (emenyCard[j].kjysTag)
					{
						kjysAtkSum += emenyCard[j].trueAtk;
						kjysDefSum += emenyCard[j].trueDef;
					}
					if (emenyCard[j].name == "光能使者")
					{
						GnszAtkSum = emenyCard[j].atk;
						GnszDefSum = emenyCard[j].def;
					}
					else if (emenyCard[j].name == "风暴使者")
					{
						FbszAtkSum = emenyCard[j].atk;
						FbszDefSum = emenyCard[j].def;
					}
					emenyCard[j].QueueFree();
					emenyCard[j] = null;
				}
			}
			else
			{
				if (playCard[i].name == "小约翰可汗")
				{
					emenyCard[j].changeAD(emenyCard[j].atk, emenyCard[j].def - playCard[i].atk);
				}
				AfterFight(playCard[i], emenyCard[j]);
			}
			playCard[i].attackTimes--;
		}
		else if ((i < 5 && playCard[i].penetrate) || (i < 5 && j >= 5))
		{
			tag = true;
			BeforeFight(playCard[i], null);
			if (i < 2)
			{
				await ToSignal(GetTree().CreateTimer(0.5f), "timeout");
				MoveToPosition(playCard[i], _e.GlobalPosition + new Vector2(50, 50), -20);
				await ToSignal(GetTree().CreateTimer(2.0), "timeout");
			}
			else if (i == 2)
			{
				await ToSignal(GetTree().CreateTimer(0.5f), "timeout");
				MoveToPosition(playCard[i], _e.GlobalPosition + new Vector2(0, 0), 0);
				await ToSignal(GetTree().CreateTimer(2.0), "timeout");
			}
			else
			{
				await ToSignal(GetTree().CreateTimer(0.5f), "timeout");
				MoveToPosition(playCard[i], _e.GlobalPosition + new Vector2(50, 50), 20);
				await ToSignal(GetTree().CreateTimer(2.0), "timeout");
			}
			playCard[i].attackTimes--;
			_e.HP -= playCard[i].atk;
			_emenyPeopleHPText.Text = "HP:" + _e.HP;
			if (_e.HP <= 0)
			{
				_p.HP += 2000;
				_playPeopleHPText.Text = "HP:" + _p.HP;
<<<<<<< HEAD
				if(count==1){
					changeCardStatus(1);
					await ToSignal(GetTree().CreateTimer(2.5), "timeout");
					EmitSignal(SignalName.MyDiaSignal, "", 3);
					waitTag=true;
					while(waitTag){
						await ToSignal(GetTree(), "process_frame");
					}
					EmitSignal(SignalName.nextEnergySignal, ++count);
					return false;
				}
				if(count==2){
					changeCardStatus(1);
					await ToSignal(GetTree().CreateTimer(2.5), "timeout");
					EmitSignal(SignalName.MyDiaSignal, "", 4);
					waitTag=true;
					while(waitTag){
						await ToSignal(GetTree(), "process_frame");
					}
					EmitSignal(SignalName.nextEnergySignal, ++count);
					return false;
				}
				if(count==3){
					_showWhiteCard.GetNode<Sprite2D>("WhiteCard").Visible=true;
=======
				if (count == 0)
				{
					_showWhiteCard.GetNode<Sprite2D>("WhiteCard").Visible = true;
>>>>>>> 5e5a1b5c372f73f20a8064c93772fa480d326604
					_showWhiteCard.Play("showHand");
					changeCardStatus(1);
					await ToSignal(GetTree().CreateTimer(2.5), "timeout");
					EmitSignal(SignalName.MyDiaSignal, "", 2);
					waitTag = true;
					while (waitTag)
					{
						await ToSignal(GetTree(), "process_frame");
					}
					_showWhiteCard.GetNode<Sprite2D>("WhiteCard").Visible = false;
					EmitSignal(SignalName.nextEnergySignal, ++count);
					return false;
				}
				if(count==7){
					_showWhiteCard.GetNode<Sprite2D>("WhiteCard").Visible=true;
					_showWhiteCard.Play("showHand");
					changeCardStatus(1);
					await ToSignal(GetTree().CreateTimer(2.5), "timeout");
					EmitSignal(SignalName.MyDiaSignal, "", 5);
					waitTag=true;
					while(waitTag){
						await ToSignal(GetTree(), "process_frame");
					}
					_showWhiteCard.GetNode<Sprite2D>("WhiteCard").Visible=false;
					EmitSignal(SignalName.nextEnergySignal, ++count);
					return false;
				}
				EmitSignal(SignalName.nextEnergySignal, ++count);
				return false;
			}
		}
		else
		{

		}
		i = 0;
		j = 0;
		while (i < 5 && playCard[i] == null) i++;
		while (j < 5 && (emenyCard[j] == null || emenyCard[j].attackTimes <= 0)) j++;
		if (i < 5 && j < 5 && !emenyCard[j].penetrate)
		{
			if (playCard[i].name == "小约翰可汗" && playCard[i].def < emenyCard[j].atk && trapGrade4 > 0)
			{
				playCard[i].changeAD(playCard[i].atk, playCard[i].def + _p.energyUseing * 500);
				while (_p.energyUseing > 0)
				{
					_p.energyUseing--;
					_p.energys[_p.energyUseing].QueueFree();
				}
				trapGrade4--;
				_trapNum[1].Text = "X" + trapGrade4;
			}
			if (trapGrade10 > 0)
			{
				int num = 0;
				for (int k = 0; k < 5; k++)
				{
					if (_e.emenyCards[k] != null) num++;
				}
				for (int k = 0; k < 5; k++)
				{
					if (_p.upCards[k] != null && _p.upCards[k].name == "耄耋")
					{
						int val = _p.upCards[k].atk / num;
						for (int m = 0; m < 5; m++)
						{
							if (_e.emenyCards[m] != null)
							{
								if (_e.emenyCards[m].def - val <= 0 || _e.emenyCards[m].atk - val <= 0)
								{
									if (btszEnemy > 0)
									{
										btszEnemy--;
									}
									else
									{
										_e.emenyCards[m].QueueFree();
										_e.emenyCards[m] = null;
									}

								}
								else
								{
									_e.emenyCards[m].changeAD(_e.emenyCards[m].atk - val, _e.emenyCards[m].def - val);
								}
							}
						}
						trapGrade10--;
						_trapNum[4].Text = "X" + trapGrade10;
					}
					if (trapGrade10 <= 0) break;
				}
			}
			tag = true;
			BeforeFight2(emenyCard[j], playCard[i]);
			if (i < j)
			{
				await ToSignal(GetTree().CreateTimer(0.5f), "timeout");
				MoveToPosition(emenyCard[j], playCard[i].GlobalPosition + new Vector2(50, 50), -20);
				await ToSignal(GetTree().CreateTimer(2.0), "timeout");
			}
			else if (i == j)
			{
				await ToSignal(GetTree().CreateTimer(0.5f), "timeout");
				MoveToPosition(emenyCard[j], playCard[i].GlobalPosition + new Vector2(0, 0), 0);
				await ToSignal(GetTree().CreateTimer(2.0), "timeout");
			}
			else
			{
				await ToSignal(GetTree().CreateTimer(0.5f), "timeout");
				MoveToPosition(emenyCard[j], playCard[i].GlobalPosition + new Vector2(50, 50), 20);
				await ToSignal(GetTree().CreateTimer(2.0), "timeout");
			}
			if (playCard[i].def < emenyCard[j].atk)
			{
				AfterFight2(emenyCard[j], playCard[i]);
				if (trapGrade2 > 0 && playCard[i].Name != "坤坤")
				{
					int k = 0;
					while (k < 5)
					{
						if (_p.upCards[k] != null && _p.upCards[k].name == "坤坤") break;
						k++;
					}
					if (k != 5)
					{
						trapGrade2--;
						_trapNum[0].Text = "X" + trapGrade2;
						_p.removeUpPlayerCard(_p.upCards[k], k);
						playCard[k] = null;
					}
					else
					{
						_p.removeUpPlayerCard(playCard[i], i);
						playCard[i] = null;
					}
				}
				else
				{
					_p.removeUpPlayerCard(playCard[i], i);
					playCard[i] = null;
				}
			}
			else
			{
				AfterFight2(emenyCard[j], playCard[i]);
			}
			emenyCard[j].attackTimes--;
		}
		else if ((j < 5 && emenyCard[j].penetrate) || (i >= 5 && j < 5))
		{
			tag = true;
			BeforeFight2(emenyCard[j], null);
			if (j < 2)
			{
				await ToSignal(GetTree().CreateTimer(0.5f), "timeout");
				MoveToPosition(emenyCard[j], _p.GlobalPosition + new Vector2(50, 50), 20);
				await ToSignal(GetTree().CreateTimer(1.0), "timeout");
			}
			else if (j == 2)
			{
				await ToSignal(GetTree().CreateTimer(0.5f), "timeout");
				MoveToPosition(emenyCard[j], _p.GlobalPosition + new Vector2(0, 0), 0);
				await ToSignal(GetTree().CreateTimer(1.0), "timeout");
			}
			else
			{
				await ToSignal(GetTree().CreateTimer(0.5f), "timeout");
				MoveToPosition(emenyCard[j], _p.GlobalPosition + new Vector2(50, 50), -20);
				await ToSignal(GetTree().CreateTimer(1.0), "timeout");
			}
			emenyCard[j].attackTimes--;
			_p.HP -= emenyCard[j].atk;
			_playPeopleHPText.Text = "HP:" + _p.HP;
			if (_p.HP < 0)
			{
				GetTree().ChangeSceneToFile("res://scene/menu.tscn");
				return false;
			}
		}
		else
		{

		}
		return tag;
	}
	public void BeforeFight(Card playCard, EmenyCard emenyCard)
	{
		if (emenyCard == null)
		{

		}
		else
		{
			if (playCard.name == "炎之魔法师" && emenyCard.attribute == "水")
			{
				playCard.changeAD(playCard.atk + 1000, playCard.def);
			}
			else if (playCard.name == "水之魔法师" && emenyCard.attribute == "炎")
			{
				playCard.changeAD(playCard.atk + 1000, playCard.def);
			}
			else if (playCard.name == "光之魔法师" && emenyCard.attribute == "暗")
			{
				playCard.changeAD(playCard.atk + 1000, playCard.def);
			}
			else if (playCard.name == "暗之魔法师" && emenyCard.attribute == "光")
			{
				playCard.changeAD(playCard.atk + 1000, playCard.def);
			}
			else if (playCard.name == "风之魔法师" && emenyCard.attribute == "地")
			{
				playCard.changeAD(playCard.atk + 1000, playCard.def);
			}
			else if (playCard.name == "地之魔法师" && emenyCard.attribute == "风")
			{
				playCard.changeAD(playCard.atk + 1000, playCard.def);
			}
			else
			{

			}



		}

	}
	public void BeforeFight2(EmenyCard emenyCard, Card playCard)
	{
		if (playCard == null)
		{

		}
		else
		{
			if (playCard.name == "地之守卫者" && emenyCard.attribute == "风")
			{
				playCard.changeAD(playCard.atk, playCard.def + 1000);
			}
			else if (playCard.name == "风之守卫者" && emenyCard.attribute == "地")
			{
				playCard.changeAD(playCard.atk, playCard.def + 1000);

			}
			else if (playCard.name == "炎之守卫者" && emenyCard.attribute == "水")
			{
				playCard.changeAD(playCard.atk, playCard.def + 1000);

			}
			else if (playCard.name == "水之守卫者" && emenyCard.attribute == "炎")
			{
				playCard.changeAD(playCard.atk, playCard.def + 1000);

			}
			else if (playCard.name == "光之守卫者" && emenyCard.attribute == "暗")
			{
				playCard.changeAD(playCard.atk, playCard.def + 1000);

			}
			else if (playCard.name == "暗之守卫者" && emenyCard.attribute == "光")
			{
				playCard.changeAD(playCard.atk, playCard.def + 1000);
			}

		}

	}

	public void AfterFight(Card playCard, EmenyCard emenyCard)
	{
		if (playCard.name == "炎之魔法师" && emenyCard.attribute == "水")
		{
			playCard.changeAD(playCard.atk - 1000, playCard.def);
		}
		else if (playCard.name == "水之魔法师" && emenyCard.attribute == "炎")
		{
			playCard.changeAD(playCard.atk - 1000, playCard.def);
		}
		else if (playCard.name == "光之魔法师" && emenyCard.attribute == "暗")
		{
			playCard.changeAD(playCard.atk - 1000, playCard.def);
		}
		else if (playCard.name == "暗之魔法师" && emenyCard.attribute == "光")
		{
			playCard.changeAD(playCard.atk - 1000, playCard.def);
		}
		else if (playCard.name == "风之魔法师" && emenyCard.attribute == "地")
		{
			playCard.changeAD(playCard.atk - 1000, playCard.def);
		}
		else if (playCard.name == "地之魔法师" && emenyCard.attribute == "风")
		{
			playCard.changeAD(playCard.atk - 1000, playCard.def);
		}
		else
		{

		}
	}

	public void AfterFight2(EmenyCard emenyCard, Card playCard)
	{

		if (playCard.name == "地之守卫者" && emenyCard.attribute == "风")
		{
			playCard.changeAD(playCard.atk, playCard.def - 1000);
		}
		else if (playCard.name == "风之守卫者" && emenyCard.attribute == "地")
		{
			playCard.changeAD(playCard.atk, playCard.def - 1000);
		}
		else if (playCard.name == "炎之守卫者" && emenyCard.attribute == "水")
		{
			playCard.changeAD(playCard.atk, playCard.def - 1000);
		}
		else if (playCard.name == "水之守卫者" && emenyCard.attribute == "炎")
		{
			playCard.changeAD(playCard.atk, playCard.def - 1000);
		}
		else if (playCard.name == "光之守卫者" && emenyCard.attribute == "暗")
		{
			playCard.changeAD(playCard.atk, playCard.def - 1000);
		}
		else if (playCard.name == "暗之守卫者" && emenyCard.attribute == "光")
		{
			playCard.changeAD(playCard.atk, playCard.def - 1000);
		}
	}
	private void MoveToPosition(Sprite2D p, Vector2 targetPosition, int rad)
	{
		var t = p.GlobalPosition;
		_moveTween = CreateTween();
		_moveTween.TweenProperty(p, "rotation", p.Rotation - Mathf.DegToRad(rad), 0.5)
			 .SetEase(Tween.EaseType.Out)
			 .SetTrans(Tween.TransitionType.Back);
		_moveTween.TweenProperty(p, "global_position", targetPosition, 0.3)
				 .SetEase(Tween.EaseType.Out)
				 .SetTrans(Tween.TransitionType.Quad);
		_moveTween.TweenProperty(p, "global_position", t, 0.3)
		 	.SetEase(Tween.EaseType.Out)
			.SetTrans(Tween.TransitionType.Quad);
		_moveTween.TweenProperty(p, "rotation", p.Rotation, 0.5)
			 .SetEase(Tween.EaseType.Out)
			 .SetTrans(Tween.TransitionType.Back);
	}

}
